<?php

/**
 * This is the model class for table "{{account_token_project}}".
 *
 * The followings are the available columns in table '{{account_token_project}}':
 * @property integer $id
 * @property string $name
 * @property string $note
 * @property integer $inCompany
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property AccountToken[] $accountTokens
 * @property User $updateUser
 * @property User $createUser
 */
class AccountTokenProject extends CActiveRecord {
	const GLOBAL_PROJECT_KEY = 'erp_account_token_project_global';
	/**
	 * Returns the static model of the specified AR class.
	 * @return AccountTokenProject the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public static function setGlobalProjectId($projectId) {
		if ($projectId) {
			Yii::app()->request->cookies[self::GLOBAL_PROJECT_KEY] = new CHttpCookie(
				self::GLOBAL_PROJECT_KEY, $projectId);
		} else {
			Yii::app()->request->cookies[self::GLOBAL_PROJECT_KEY] = new CHttpCookie(
				self::GLOBAL_PROJECT_KEY, null);
			unset(Yii::app()->request->cookies[self::GLOBAL_PROJECT_KEY]);
		}
	}

	public static function getGlobalProjectId() {
		return (isset(Yii::app()->request->cookies[self::GLOBAL_PROJECT_KEY])) ? Yii::app()
				->request->cookies[self::GLOBAL_PROJECT_KEY]->value : 1;
	}
	public static function getGlobalProjectName() {
		if ($projectId = self::getGlobalProjectId()) {
			return self::model()->findByPk($projectId)->name;
		} else {
			return '没有选择账本';
		}
	}

	public static function getAll() {
		return self::model()
			->cache(YII_DEBUG ? 1 : 3600,
				new CDbCacheDependency('select max(updateTime) from erp_account_token_project'))
			->findAll(array(
				'order' => 'id ASC'
			));
	}

	public static function getCompanyProjectIds() {
		$projects = self::model()
			->cache(YII_DEBUG ? 1 : 3600,
				new CDbCacheDependency('select max(updateTime) from erp_account_token_project'))
			->findAll(array(
				'condition' => 'inCompany=1',
				'order' => 'id ASC'
			));
		return CHtml::listData($projects, 'id', 'id');
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{account_token_project}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'name, inCompany',
				'required'
			),
			array(
				'inCompany',
				'numerical',
				'integerOnly' => true
			),
			array(
				'name, note',
				'length',
				'max' => 255
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, name, note, inCompany, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'accountTokens' => array(
				self::HAS_MANY,
				'AccountToken',
				'projectId'
			),
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'name, note, inCompany'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'name' => '凭证项目',
			'note' => '备注',
			'inCompany' => '公司内账本',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('inCompany', $this->inCompany);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
}
